<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <!-- http://www.javashuo.com/article/p-hngnrnda-ma.html -->
    <script>
        // 微信红包拆分测试
        let money = 1;  //单位（元）
        let num = 54;
        console.log("总金额：" + money + "元\t 拆分成包总个数:" + num);
        let array = divived(money, num);

        console.log("拆分后的各个红包金额数以下:");
        array.forEach(value=>{
            console.log(value);
        })

        // 将money分成n份
        function divived(money, n) {
            let fen = money * 100;
            if (fen < n || fen < 1) {
                throw new Error("被拆分的总金额不能小于1分");
            }
            // 建立一个长度等于n的红包数组
            let array = new Array(n);
            //第一步 每一个红包先塞1分
            array.fill(1);
            // console.log('array', array);
            //总金额减去已分配的n 分钱
            fen -= n;
            // console.log('fen', fen);

            //第二步，循环遍历若是剩余金额>0 则一直分配
            let i = 0;  //从第一个红包进行分配

            //建立一个随机分配对象
            while (fen > 1) {
                let f = randOne(0, fen);  //建立范围[0,fen)
                let index = ++i;
                index = index % n;
                // console.log(`i-${i} index-${index}`);
                array[index] += f;
                fen -= f;
                // console.log(array);
                // console.log('fen', fen);
            }
            let res = 0;
            array.forEach(value => {
                res += value;
            })
            // console.log('res', res);
            //判断剩余未分配的金额是否大于0,若是大于0，能够把剩下未分配金额塞到第一个红包中
            if (fen > 0) {
                array[0] += fen;
            }
            return array;
        }

        // 获取闭区间 [n, m) 内的一个随机整数
        function randOne(n, m) {
            return Math.floor(Math.random() * (m - n)) + n;
        };
    </script>
    <script>
        let radomNums = [1, 4, 19, 2, 38, 51, 6, 37, 15, 9, 45, 43, 7, 52, 16, 21, 39, 53, 24, 26, 44, 54, 40, 5, 32, 10, 46, 22, 33, 27, 3, 11, 18, 28, 47, 12, 17, 29, 8, 13, 41, 30, 48, 14, 34, 31, 20, 23, 49, 35, 25, 42, 50, 36];
        // // 这个生成参考分拼手气红包
        // let arr2=divived(money, num); 

        // 见../2-抢红包/1-二倍均值法.html
        let arr2=[0.03, 0.03, 0.01, 0.01, 0.02, 0.01, 0.01, 0.02, 0.03, 0.01, 0.01, 0.02, 0.01, 0.01, 0.02, 0.01, 0.02, 0.03, 0.01, 0.02, 0.02, 0.03, 0.01, 0.02, 0.02, 0.03, 0.02, 0.01, 0.03, 0.03, 0.03, 0.01, 0.02, 0.01, 0.02, 0.01, 0.01, 0.02, 0.03, 0.03, 0.03, 0.01, 0.01, 0.01, 0.02, 0.01, 0.02, 0.01, 0.02, 0.02, 0.03, 0.03, 0.02, 0.01];
        let probabilityNums =arr2.map(value=>{
            value=value/100;
            console.log(value);
        });// 共 54 项，和为 1

        function randomProbability(arr1, arr2) {
            var sum = 0,
                factor = 0,
                random = Math.random();
            for (let i = arr2.length - 1; i >= 0; i--) {
                sum += arr2[i]; // 统计概率总和
            };
            random *= sum; // 生成概率随机数
            for (let i = arr2.length - 1; i >= 0; i--) {
                factor += arr2[i];
                if (random <= factor) return arr1[i]; // 如果在当前的概率范围内，得到的就是当前概率，返回输出
            };
            return null;
        }

        const yourCard = randomProbability(radomNums, probabilityNums)

        console.log(yourCard)

    </script>

</body>

</html>